package com.demo.rocketmq.controller;

import io.swagger.annotations.Api;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.StandardCharsets;

/**
 * @author huang
 */
@RequestMapping("rocketmq")
@RestController
@Api(tags = "Rocketmq作业--生产者")
public class RocketmqController {

    @GetMapping("producer")
    public String producer() throws Exception {
        DefaultMQProducer producer = new DefaultMQProducer("provider");
        producer.setNamesrvAddr("127.0.0.1:9876");
        producer.start();
        int total = 100;
        for (int i = 0; i < total; i++) {
            Message message = new Message("TopicTest", "TagA", ("Hello RocketMq" + i).getBytes(StandardCharsets.UTF_8));
            SendResult sendResult = producer.send(message);
            System.out.printf("%s%n", sendResult);
        }
        producer.shutdown();
        return "发送成功";
    }


}
